home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / enscript.4 / enscript / enscript-1.4.0 / src / mkafmmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-30  |  4.0 KB  |  223 lines

  1. /* 
  2.  * Create font map for AFM files.
  3.  * Copyright (c) 1995 Markku Rossi.
  4.  *
  5.  * Author: Markku Rossi <mtr@iki.fi>
  6.  */
  7.  
  8. /*
  9.  * This file is part of GNU enscript.
  10.  * 
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2, or (at your option)
  14.  * any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; see the file COPYING.  If not, write to
  23.  * the Free Software Foundation, 59 Temple Place - Suite 330,
  24.  * Boston, MA 02111-1307, USA.
  25.  */
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include <stdio.h>
  32.  
  33. #if HAVE_STDLIB_H
  34. #include <stdlib.h>
  35. #endif
  36.  
  37. #if HAVE_STRING_H
  38. #include <string.h>
  39. #endif
  40.  
  41. #include "afm.h"
  42. #include "getopt.h"
  43.  
  44.  
  45. /*
  46.  * Definitions.
  47.  */
  48.  
  49. #define HANDLE_ERROR(msg)                    \
  50.   if (error != AFM_SUCCESS)                    \
  51.     {                                \
  52.       char buf[256];                        \
  53.       afm_error_to_string (error, buf);                \
  54.       fprintf (stderr, "%s: %s: %s\n", program, msg, buf);    \
  55.       exit (1);                            \
  56.     }
  57.  
  58.  
  59. /*
  60.  * Prototypes for static functions.
  61.  */
  62.  
  63. static void usage ();
  64.  
  65.  
  66. /*
  67.  * Static variables.
  68.  */
  69.  
  70. /* Options. */
  71.  
  72. /*
  73.  * --output-file, -p
  74.  *
  75.  * The name of the file to which font map is stored.  If name is NULL,
  76.  * leaves output to stdout.
  77.  */
  78. static char *fname = "font.map";
  79.  
  80.  
  81. static char *program;
  82.  
  83. static struct option long_options[] =
  84. {
  85.   {"output-file",    required_argument,    0, 'p'},
  86.   {"help",        no_argument,        0, 'h'},
  87.   {NULL, 0, 0, 0},
  88. };
  89.  
  90. /*
  91.  * Global functions.
  92.  */
  93.  
  94. int 
  95. main (int argc, char *argv[])
  96. {
  97.   AFMError error;
  98.   AFMHandle afm;
  99.   AFMFont font;
  100.   int i;
  101.   FILE *ofp;
  102.   FILE *mfp;
  103.  
  104.   program = strrchr (argv[0], '/');
  105.   if (program == NULL)
  106.     program = argv[0];
  107.   else
  108.     program++;
  109.  
  110.   /* Make getopt_long() to use our modified programname. */
  111.   argv[0] = program;
  112.  
  113.   /* Handle arguments. */
  114.   while (1)
  115.     {
  116.       int option_index = 0;
  117.       int c;
  118.       
  119.       c = getopt_long (argc, argv, "p:h", long_options, &option_index);
  120.       if (c == EOF)
  121.     break;
  122.  
  123.       switch (c)
  124.     {
  125.     case 'h':        /* help */
  126.       usage ();
  127.       exit (0);
  128.       
  129.     case 'p':        /* output file */
  130.       /* Check output file "-". */
  131.       if (strcmp (optarg, "-") == 0)
  132.         fname = NULL;
  133.       else
  134.         fname = optarg;
  135.       break;
  136.  
  137.     case '?':        /* errors in arguments */
  138.       usage ();
  139.       exit (1);
  140.       break;
  141.     }
  142.     }
  143.  
  144.   /* Open output file. */
  145.   printf ("file=%s\n", fname ? fname : "stdout");
  146.   if (fname)
  147.     {
  148.       ofp = fopen (fname, "w");
  149.       if (ofp == NULL)
  150.     {
  151.       char buf[256];
  152.  
  153.       sprintf (buf, "%s: couldn't open output file \"%s\"",
  154.            program, fname);
  155.       perror (buf);
  156.       exit (1);
  157.     }
  158.       mfp = stdout;
  159.     }
  160.   else
  161.     {
  162.       ofp = stdout;
  163.       mfp = stderr;
  164.     }
  165.  
  166.   error = afm_create (NULL, 0, &afm);
  167.   HANDLE_ERROR ("couldn't create AFM library");
  168.  
  169.   for (i = optind; i < argc; i++)
  170.     {
  171.       fprintf (mfp, "%s...\n", argv[i]);
  172.       error = afm_open_file (afm, AFM_I_MINIMUM, argv[i], &font);
  173.       if (error == AFM_SUCCESS)
  174.     {
  175.       char *cp;
  176.       char *sf;
  177.       int len;
  178.  
  179.       cp = strrchr (argv[i], '/');
  180.       if (cp == NULL)
  181.         cp = argv[i];
  182.       else
  183.         cp++;
  184.  
  185.       sf = strrchr (argv[i], '.');
  186.       if (sf)
  187.         len = sf - cp;
  188.       else
  189.         len = strlen (cp);
  190.  
  191.       fprintf (ofp, "%-30s\t%.*s\n", font->global_info.FontName, len, cp);
  192.       (void) afm_close_font (font);
  193.     }
  194.       else
  195.     {
  196.       char buf[256];
  197.       afm_error_to_string (error, buf);
  198.       fprintf (mfp, "%s: %s\n", program, buf);
  199.     }
  200.     }
  201.  
  202.   if (fname)
  203.     fclose (ofp);
  204.   
  205.   return 0;
  206. }
  207.  
  208.  
  209. /*
  210.  * Static functions.
  211.  */
  212.  
  213. static void
  214. usage ()
  215. {
  216.   printf ("Usage: %s [options] file [files ...]\n\
  217.   Options:\n\
  218.     -h, --help            print this help and exit\n\
  219.     -pNAME, --output-file=NAME     print output to file NAME (default file\n\
  220.                 is font.map)\n\
  221.     -p -, --output-file -    print output to stdout\n", program);
  222. }
  223.